/**
 * 使用Express提供接口，供外部使用。同时也供内部使用。
 */
var bodyParser = require("body-parser");
const apiRouter = require("./admin/users/apiRouter.ts");
const depart = require("./admin/departs/DepartmentsApi.ts");
const company = require("./admin/departs/CompanysController.ts");
const roleAccess = require("./admin/roleAccess/RoleAccessApi.ts");
const storeShop = require("./business/store-shop/index.ts");
const webApi = require("./business/web/index.ts");
const openCreate = require("./business/post/index.ts");
const objectConfig = require("./baseconfig/index.ts");
const core = require("./baseconfig/core.ts");
const files = require("./business/file/index.ts");
const ApiList = require("./whiteList.ts");
const findAllApi = require("../controller/findAllApi");

const mobileLogin = require("./mobile/mobileLogin.ts");
function setApis(app) {
  const filesList = findAllApi();
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: false }));
  app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header(
      "Access-Control-Allow-Headers",
      "Origin, X-Requested-With, Content-Type, Accept"
    );
    next();
  });

  app.use(function (req, res, next) {
    const legal = ApiList(req.url, filesList);
    if (legal === 0) {
      next();
    } else if (legal === 1) {
      if (req.session && req.session.user) {
        next();
      } else {
        res.send({
          code: 20202,
          msg: "登录信息已经过期、请重新登录！",
          data: null,
        });
      }
    } else {
      res.send({
        code: 404,
        msg: "非法登录！",
        data: null,
      });
    }
  });
  app.use("/rest/mobile", mobileLogin); //移动端注册登录
  app.use("/rest", apiRouter); //web端注册登录，查看租户
  app.use("/rest", depart); //部门接口。
  app.use("/rest", company); //公司，
  app.use("/rest", roleAccess); //角色信息。
  app.use("/rest", storeShop);
  app.use("/rest", webApi);
  app.use("/rest", openCreate);
  app.use("/rest", files);
  app.use("/base", objectConfig);
  app.use("/rests", core);
}

module.exports = setApis;
